package kaaengine.loaders
{
	import flash.display.BitmapData;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	
	import kaaengine.display.BitmapDataRenderer;
	
	import mx.collections.ArrayCollection;
	import mx.rpc.IResponder;

	[Event(name="images_loaded", type="flash.events.Event")]
	
	
	public class ImageLoader extends EventDispatcher implements IResponder
	{
		public var renderables:ArrayCollection;
		private var num:int = 0;
		private var max:int = 5;
		private var tab:Array;
		private var rep:String;
		private var tabIndex:int = 0;
		public function ImageLoader()
		{			
		}
		
		
		public function loadImagesFromArray(_rep:String=null, _tab:Array=null):void {
			if( tab==null) { 
				tab = _tab;
				tabIndex = 0;
			}			
			if(_rep != null ) { 
				rep = _rep;
			}
			var fah :FileAccessHelper = new FileAccessHelper(this);
			fah.loadPNGFile(rep+tab[tabIndex]);	
		}
		

		public function result(data:Object):void
		{
			var bitmap:BitmapData = data as BitmapData;
			var bdr:BitmapDataRenderer = new BitmapDataRenderer();
			bdr._bitmap = bitmap;
			renderables.addItem(bdr);
			bdr.name = tab[tabIndex];
			trace("bdr.name="+bdr.name);
			tabIndex++;
			if( tabIndex != tab.length ) {					
				loadImagesFromArray();
			} else {				
				dispatchEvent(new Event("images_loaded"));				
			}					
		}
		
		public function fault(info:Object):void
		{
		}
		
	}
}